package org.bspv.pharma.dao.impl;

import java.util.Date;
import java.util.List;

import org.bspv.pharma.dao.StandardEquipmentDAO;
import org.bspv.pharma.model.Place;
import org.bspv.pharma.model.StandardEquipment;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 
 * @author guillaume
 * 
 */
@Repository(value = "standardEquipmentDAO")
public class StandardEquipmentDAOImpl extends
		AbstractHibernateDAO<StandardEquipment, Long> implements
		StandardEquipmentDAO {

	@Autowired
	public StandardEquipmentDAOImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	public StandardEquipment findStandardEquipment(Place place) {
		Date today = new Date();
		StandardEquipment res = null;
		DetachedCriteria crit = DetachedCriteria.forClass(getManagedType());
		crit.add(Restrictions.eq("place", place));
		crit.add(Restrictions.ge("beginningOfValidity", today));
		crit.add(Restrictions.le("endOfValidity", today));
		List<StandardEquipment> lst = getHibernateTemplate().findByCriteria(
				crit);
		if (lst != null && lst.size() > 0) {
			res = lst.get(0);
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	public List<StandardEquipment> findStandardEquipments(List<Place> places) {
		Date today = new Date();
		DetachedCriteria crit = DetachedCriteria.forClass(getManagedType());
		crit.add(Restrictions.in("place", places));
		crit.add(Restrictions.le("beginningOfValidity", today));
		crit.add(Restrictions.ge("endOfValidity", today));
		List<StandardEquipment> lst = getHibernateTemplate().findByCriteria(
				crit);
		return lst;
	}

}
